package com.woniu.order.controller;


import com.woniu.entity.Result;
import com.woniu.order.entity.dto.DisplayChooseProductByUserDTO;
import com.woniu.order.pojo.Order;
import com.woniu.order.service.IOrderService;
import com.woniu.pay.feign.AlipayFeignClient;
import com.woniu.user.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.woniu.entity.Result;
import com.woniu.order.entity.dto.QueryServiceOrderDto;
import com.woniu.order.service.IOrderService;
import com.woniu.order.service.impl.OrderServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.order.entity.OrderEndQuery;
import com.woniu.order.pojo.Order;
import com.woniu.order.service.IOrderService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.order.entity.OrderEndQuery;
import com.woniu.order.pojo.Order;
import com.woniu.order.service.IOrderService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单管理")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("/add/{username}")
    @ApiOperation("添加订单")
    public Result addOrder(@PathVariable String username, @RequestBody DisplayChooseProductByUserDTO displayChooseProductByUserDTO) throws Exception{
//解析token
//        String token = request.getHeader("Authorization");
//        Claims claims = JwtUtil.parseJWT(token);
//        String username = (String) claims.get("username");
        //调用service的下单操作
        String form = orderService.displayChooseProductsByUser(displayChooseProductByUserDTO,username);
        //3.调用支付微服务
        return Result.ok(form);
    }

    @PostMapping("/select/{username}")
    @ApiOperation("根据用户名查询订单")
    public Result<List<Order>> selectOrderListByUsername(@PathVariable String username){
        List<Order> orderListByUsername = orderService.selectOrderListByUsername(username);
        return Result.ok(orderListByUsername);
    }

    @PostMapping("/select/{orderNo}/{username}")
    @ApiOperation("根据用户名查询订单")
    public Result<Order> selectOrderByOrderNOAndUsername(@PathVariable String orderNo, @PathVariable String username){
        Order order = orderService.selectOrderByOrderNOAndUsername(orderNo,username);
        return Result.ok(order);
    }

//    @GetMapping("/{userId}")
//    @ApiOperation("根据userId查询订单信息")
//    public Result<List<Order>> getOrderInfoByUserId(@PathVariable String userId){
//        List<Order> orderInfoByUserIdList = orderService.getOrderInfoByUserId(userId);
//        return Result.ok(orderInfoByUserIdList);
//    }

    @PostMapping ("/queryServiceOrder/{currentPage}/{currentSize}")
    @ApiOperation("门店查询订单列表")
    public Result queryServiceOrder(@PathVariable Integer currentPage,@PathVariable Integer currentSize, @RequestBody QueryServiceOrderDto queryServiceOrderDto){
        return orderService.queryServiceOrder(currentPage,currentSize,queryServiceOrderDto);
    }

    @ApiOperation("门店查询具体订单")
    @GetMapping("/shopQueryOrder/{shopId}/{no}")
    public Result shopQueryOrder(@PathVariable String shopId,@PathVariable String no){
        return orderService.shopQueryOrder(shopId,no);
    }

    @PostMapping ("/queryServiceStatistics/{currentPage}/{currentSize}")
    @ApiOperation("门店查询服务统计接口")
    public Result queryServiceStatistics(@PathVariable Integer currentPage,@PathVariable Integer currentSize, @RequestBody QueryServiceOrderDto queryServiceOrderDto){
        return orderService.queryServiceStatistics(currentPage,currentSize,queryServiceOrderDto);
    }

    @ApiOperation("后台订单查询")
    @PostMapping("/queryOrderThroughBackEnd/{currentPage}/{pageSize}")
    public Result<PageResult<Order>> queryOrderThroughBackEnd(@PathVariable Integer currentPage ,
                                                       @PathVariable Integer pageSize ,
                                                       @RequestBody OrderEndQuery orderEndQuery){
        PageResult<Order> pageResult = orderService.queryOrderThroughBackEnd(currentPage,pageSize,orderEndQuery);
        return Result.ok(pageResult);
    }
}
